<template>
  <view class="common setting pos-r">
    <view class="pos-a z-0 wp-100" :class="env === 'H5' ? 'bg-yel' : 'bg-lg'" :style="globalData.barStyle" />
    <u-navbar :fixed="false">
      <u-icon slot="left" name="arrow-left" :color="env === 'H5' ? '#fff' : '#000'" size="19" @tap="navigateBack" />
      <view slot="center" :class="[env === 'H5' ? 'c-fff' : 'c-bla']">设置</view>
    </u-navbar>
    <view class="main bg-gry">
      <view class="bg-whi">
        <u-cell-group>
          <u-cell title="修改密码" @click="gotoChangePassword" isLink url="/pages/global/my/changePassword" />
          <u-cell title="注销账号" @click="showDissolutionConfirm" />
          <u-cell title="退出登录" @click="showLogoutConfirm" />
        </u-cell-group>
      </view>
    </view>
  </view>
</template>
<script>
import commonMxins from '@/mixins/common.js';
export default {
  mixins: [commonMxins],
  data() {
    return {};
  },
  onLoad() {},
  methods: {
    // 跳转到修改密码页面
    gotoChangePassword() {
      uni.navigateTo({ url: '/pages/my/changePassword' });
    },
    // 显示注销确认框
    showDissolutionConfirm() {
      uni.showModal({
        title: '注销账号',
        content: '确定要注销账号吗？注销后将无法恢复您的账号信息。',
        confirmColor: '#fea20d',
        success: res => {
          if (res.confirm) {
            this.dissolutionAccount();
          }
        }
      });
    },
    // 注销账号
    async dissolutionAccount() {
      const res = await this.$request({ url: '/app/appUser/dissolution', method: 'POST', showLoading: true });
      if (res.code === 200) {
        uni.showToast({ title: '账号注销成功', icon: 'success' });
        await this.$async.delay(2000);
        this.clearStorage();
        uni.reLaunch({ url: '/pages/global/login/login' });
      } else {
        uni.showToast({ title: res.msg || '账号注销失败', icon: 'none' });
      }
    },
    // 显示退出登录确认框
    showLogoutConfirm() {
      uni.showModal({
        title: '退出登录',
        content: '确定要退出登录吗？',
        confirmColor: '#fea20d',
        success: res => {
          if (res.confirm) {
            this.logout();
          }
        }
      });
    },
    // 退出登录
    async logout() {
      const res = await this.$request({ url: '/app/appUser/logout', method: 'POST', showLoading: true });
      this.clearStorage();
      uni.reLaunch({ url: '/pages/global/login/login' });
      if (res.code === 200) {
        uni.showToast({ title: '退出登录成功', icon: 'success' });
      }
    }
  }
};
</script>
